package com.android.util;

public class Time {

	private int day;
	private String strTime;
	private int mintime;
	private int hour;
	private int minute;
	
	public Time() {
		this.day = 0;
		this.hour = 0;
		this.minute = 0;
		this.mintime = hour + minute;
	}
	
	public Time(String time) {
		//TODO: check String format
		String[] timeArray = time.split(":");
		int hourToMin = Integer.parseInt(timeArray[0]) * 60;
		int minute = Integer.parseInt(timeArray[1]);		
		
		this.day = 0;
		this.mintime = hourToMin + minute;
		this.hour = Integer.parseInt(timeArray[0]);
		this.minute = Integer.parseInt(timeArray[1]);
		this.strTime = this.hour + ":" + this.minute; 
	}
	
	public Time(int time) {
		if (time < 0)
			return;
		
		this.mintime = time;
		this.hour = time / 60;
		this.minute = time % 60;
		this.strTime = this.hour + ":" + this.minute;
	}
	
	public void setTime(String time) {
		String[] timeArray = time.split(":");
		int hour = Integer.parseInt(timeArray[0]) * 60;
		int minute = Integer.parseInt(timeArray[1]);		
		this.mintime = hour + minute;		
	}
		
	public String getTime() {
		return strTime;
	}
	
	public int getMinTime() {
		return this.mintime;
	}
	
	public void setMinTime(int mintime) {
		this.mintime = mintime;
		this.hour = mintime / 60;
		
		if (this.hour > 24) {
			this.hour -= 24;
			this.day += 1;
		}
		
		this.minute = mintime % 60;
	}
	
	public String toString() {
		return strTime;
	}
	
	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public void add(Time t) {		
		int mintime = t.getMinTime() + this.getMinTime(); 
		int hour = mintime / 60;
		
		if (hour > 24) {
			hour -= 24;
			this.day += 1;
			this.hour = hour;
		}
			 
		this.setMinute(mintime % 60);
	}

	public void subtract(Time t) {
		//TODO: if it receives a time from a day in the future, it wont work
		if (t.mintime < this.mintime) {
			return;
		}
		
		int difference = this.mintime - t.mintime;
		this.setHour(difference / 60);
		this.setHour(difference % 60);
	}

	/**
	 * Subtracts t2 from t1.
	 * **/
	public static Time subtract(Time t1, Time t2) {
		Time result = new Time();
		
		if (t1.getMinTime() < t2.getMinTime()) {
			return result;
		}
		
		int mintime = t1.getMinTime() - t2.getMinTime();
		result.setMinTime(mintime);				
		return result;
	}
	
	/**
	 * Adds t2 to t1
	 * **/
	public static Time add(Time t1, Time t2) {
		Time result = new Time();
		
		int mintime = t1.getMinTime() + t2.getMinTime();
		result.setMinTime(mintime);
		
		return result;
	}
	
	/**
	 * Compare two Time objects.
	 * @return 0 if the objects are equal
	 * @return 1 if object 2 is greater
	 * @return -1 if object 2 is smaller
	 * **/
	public static int compare(Time t1, Time t2) {
		if (t1.getDay() > t2.getDay())
			return 1;
		if (t1.getDay() < t2.getDay())
			return -1;
		if (t1.getMinTime() == t2.getMinTime())
			return 0;
		else if (t1.getMinTime() < t2.getMinTime())
			return -1;
		else
			return 1;
	}
	
}
